package com.chapter6;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ServletConText1 extends HttpServlet {
    private ServletConfig config;
    private ServletContext context;
    @Override
    public void init(ServletConfig config) throws ServletException{
        super.init(config);
        this.config = config;
    }
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        context = this.config.getServletContext();
        String url = this.context.getInitParameter("url");
        String username = this.context.getInitParameter("username");
        String password = this.context.getInitParameter("password");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        //输出的HTML正文数据
        out.println("<HTML>");
        out.println("<HEAD><TITLE>ServletContext</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("获取连接数据库的初始化参数:"+"<br/>");
        out.println("url:"+url+"<br/>");
        out.println("username:"+username+"<br/>");
        out.println("username:"+password+"<br/>");
        out.println(" <BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        doPost(request,response);
    }
}
